<?php
namespace ApiBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use ApiBundle\Controller\ApiCommonController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use CommonBundle\Controller\IAuth;
use Symfony\Component\HttpFoundation\Response;



/**
 * Class ApiUserController
 * @package ApiBundle\Controller
 */
class ApiNewsController extends ApiCommonController{


    /**
     * @abstract 获取首页头图和对应的新闻信息
     * @Route("/news/header",name="news_header")
     */
    public function getNewsHeader(Request $request){
        //获取请求传递的参数
        $_arrCondition = $request->query->all();
        foreach($_arrCondition as $key=>$value){
            if(is_null($value) || $value == '' ||  $value == null){
                unset($_arrCondition[$key]);
            }
        }
        $_intFrom = ($_arrCondition['from'] != '')?$_arrCondition['from']:0;
        $_intSize = ($_arrCondition['size'] != '')?$_arrCondition['size']:3;
        //查询首页显示的头图
        $objManager = $this->getDoctrine()->getManager();
        $_strSql = 'select news.id,news.title,news.smallTitle,news.image,news.caiid from AppBundle:EntNews news where news.isHeadFigure = :isheadfigure';
        $_arrCondition = array('isheadfigure' => 1);
        $_arrIsFigureResult = $objManager->getRepository('AppBundle:EntNews')->getDataByConition($_strSql,$_arrCondition,0,4);
        //查询对应的推荐新闻
        $_strSql = 'select news.id,news.title,news.smallTitle,news.image,news.caiid,news.createTime,cai.caiitem from AppBundle:EntNews news,AppBundle:EntCai cai where news.caiid = cai.id and news.isPosition = :isposition';
        $_arrCondition = array('isposition' => 1);
        $_arrIsPositionResult = $objManager->getRepository('AppBundle:EntNews')->getDataByConition($_strSql,$_arrCondition,$_intFrom,$_intSize);
        $_arrReturn['header'] = $_arrIsFigureResult;
        $_arrReturn['header_count'] = count($_arrIsPositionResult);
        //在赋值之前将时间戳转换成具体的时间
        foreach($_arrIsPositionResult as $key=>$value){
            $_arrIsPositionResult[$key]['createTime'] = date('Y-m-d H:i:s',$value['createTime']);
        }
        $_arrReturn['position'] = $_arrIsPositionResult;
        $_arrReturn['position_count'] = count($_arrIsPositionResult);
        $_strReturnJson = $this->get('common_service')->apiEnCode('1','查询成功',$_arrReturn,200);
        return new Response($_strReturnJson);
    }


    /**
     * @abstract 查询特定的新闻页面
     * @Route("/news/list/info",name="new_list_info")
     */
    public function getNewsInfo(Request $request){
        //获取路由传递过来的参数
        $_arrCondition = array();
        $_arrCondition = $request->query->all();
        foreach($_arrCondition as $key=>$value){
            if(is_null($value) || $value == '' ||  $value == null){
                unset($_arrCondition[$key]);
            }
        }
        //设置分页的查询起点，查询条数和当前页,这里现在是手动配置的，可以集成到父级的控制器中
//        $this->getPageAndSize($_arrCondition);
        $_intFrom = ($_arrCondition['from'] != '')?$_arrCondition['from']:0;
        $_intSize = ($_arrCondition['size'] != '')?$_arrCondition['size']:3;
        unset($_arrCondition['page']);
        $em = $this->getDoctrine()->getManager();
        //获取需要查询的sql和对应的条件
        $arrSqlAndCondition = $em->getRepository('AppBundle:EntNews')->getSql($_arrCondition);
        $_strSql = $arrSqlAndCondition['sql'];
        unset($arrSqlAndCondition['sql']);
        $arrNews = $em->getRepository('AppBundle:EntNews')->getDataByConition($_strSql,$arrSqlAndCondition,$_intFrom,$_intSize);
        //在赋值之前将时间戳转换成具体的时间
        foreach($arrNews as $key=>$value){
            $arrNews[$key]['createTime'] = date('Y-m-d H:i:s',$value['createTime']);
        }
        $intCountNews = $em->getRepository('AppBundle:EntNews')->getDataByConditionCount($_strSql,$arrSqlAndCondition);
        //组织返回数组
        $_arrReturn  = [
            'total' => $intCountNews,
            'newlist' => $arrNews,
            'newlist_count' => count($arrNews)
        ];
        $strReturn = $this->get('common_service')->apiEnCode('1','查询成功',$_arrReturn,200);
        return new Response($strReturn);

    }

    /**
     * @abstract 根据阅读量查询排行信息
     * @Route("/news/read_count",name="news_info")
     */
    public function getNewsInfoByReadCount(){
        $em = $this->getDoctrine()->getManager();
        $arrNews = $em->getRepository('AppBundle:EntNews')->getNewsDataByRank();
        $strReturn = $this->get('common_service')->apiEnCode('1','查询成功',$arrNews,200);
        return new Response($strReturn);
    }

    /**
     * @abstract 获取新闻的详情页
     * @Route("/news/detail/{id}",name="news_detail_info")
     */
    public function getNewsDetailInfo($id){
        $em = $this->getDoctrine()->getManager();
        try{
            $arrNews = $em->getRepository('AppBundle:EntNews')->getNewsDetailInfo($id);
            $_arrFirstNews = array_shift($arrNews);
            if(count($_arrFirstNews) == 0){//对象为空
                $strReturn = $this->get('common_service')->apiEnCode('1','查询成功',$_arrFirstNews,200);
            }
            //去掉content中的html标签
            $_arrFirstNews['content'] = strip_tags($_arrFirstNews['content']);
            $strReturn = $this->get('common_service')->apiEnCode('1','查询成功',$_arrFirstNews,200);
            //查询执行成功之后将对应的阅读数量新增1
            $_objNews = $em->getRepository('AppBundle:EntNews')->find($id);
            $_objNews->setReadCount($_objNews->getReadCount() + 1);
            $em->flush();
        }catch(\Exception $e){
            $strReturn = $this->get('common_service')->apiEnCode('0',$e->getMessage(),'',400);
        }
        return new Response($strReturn);
    }


    /**
     * @abstract 测试路由
     * @Route("/news/test",name="news_test")
     */
    function test(Request $request){
        //验证数据请求头
        $arrData = [
            'id' => '1',
            'did' =>'123123123',
            'time' => $this->get('common_service')->get13TimeStamp()
        ];

        //加密数据VjCFzLBaPLjoSWPYVho0BO0ZP7YBh3tENkvvzsRp+DRmnFEl0iHueoGCj0C0RDVFhkNVqtX8RHttsPpGJqXtiA==
        $_strSign = $this->get('common_service')->encryptSign($arrData);
        echo $_strSign;exit;
    }


}